<?php
if(!defined('PATH')) exit();

class Model_StoreItems extends SimpleO {

	private $data = array();
	private $index = -1;

	public function __construct() {
		parent::__construct();
	}

	public function load($type) {
		$this->data = array();
		$this->index = -1;
		$temp = $this->db->query('SELECT `__archez_store_items`.`id`, `__archez_store_items`.`name`, `__archez_store_items`.`price`, `__archez_store_items`.`unit`, `__archez_store_items`.`amount`, `__archez_store_items`.`type` FROM `__archez_store_items` WHERE `__archez_store_items`.`type` = '.$this->db->quote($type).' ORDER BY `name` DESC')->fetchAll();
		foreach ($temp as $key => $value) {
			$this->data[$key] = new Model_Store();
			foreach ($value as $inner_key => $inner_value) {
				$this->data[$key]->$inner_key = $inner_value;
			}
		}
		if ($this->count() > 0)
			return true;
		return false;
	}

	public function next() {
		$this->index++;
		if ($this->index >= count($this->data))
			return false;
		return $this->data[$this->index];
	}

	public function count() {
		return count($this->data);
	}

	public function countAll() {
		$count = $this->db->query('SELECT COUNT(*) as `n`  FROM `__archez_store_items`;')->fetch();
		return $count['n'];
	}

	public function isLoaded() {
		if ($this->count() > 0) {
			return true;
		}
		return false;
	}
}

?>